# 整个 flask_demo 是一个应用，整个应用从外部进行引用，因此创建为一个 python_package
# 这个文件就是整个模块被 import 的时候所执行的代码
# 所有关于程序的代码都写在这里
from flask import Flask
from flask_cors import CORS

# 创建这个应用的实例对象
# 这个文件里创建的对象，是可以被 import 的，比如下面的 app 对象
app = Flask(__name__)

# 打印当前执行的文件名
print("flask_demo/__init__.py is running. __name__ is ", __name__)


@app.route("/")
@app.route("/index")
def get_index():
    return "Hello,Flask."


# 数据库声明
from flask_sqlalchemy import SQLAlchemy

# app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:louisroot1234@59.67.246.128:3306/component_edit?charset=utf8mb4'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test_louis.db'
# app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root@localhost:3306/component_edit?charset=utf8mb4'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app_database = SQLAlchemy(app)

from flask_demo.entities import User


# 如果不存在数据表，则创建数据表，这里在第一次运行之后需要注释掉
def init_database():
    app_database.drop_all()
    app_database.create_all()
    the_first = User("Louis", "123456", 0)
    app_database.session.add(the_first)
    app_database.session.commit()


# 注册所有声明了的蓝图
from flask_demo.blue_print_all.user_blue_print import user_blue_print
app.register_blueprint(blueprint=user_blue_print)
from flask_demo.blue_print_all.file_blue_print import file_blue_print
app.register_blueprint(blueprint=file_blue_print)
from flask_demo.blue_print_all.img_component_blue_print import img_component_blue_print
app.register_blueprint(blueprint=img_component_blue_print)
from flask_demo.blue_print_all.img_work_blue_print import img_work_blue_print
app.register_blueprint(blueprint=img_work_blue_print)
from flask_demo.blue_print_all.logic_component_blue_print import logic_component_blue_print
app.register_blueprint(blueprint=logic_component_blue_print)
from flask_demo.blue_print_all.logic_work_blue_print import logic_work_blue_print
app.register_blueprint(blueprint=logic_work_blue_print)
from flask_demo.blue_print_all.function_blue_print import function_blue_print
app.register_blueprint(blueprint=function_blue_print)

# 设定所有路由跨域访问
# CORS(app, resources=r'/*', supports_credentials=True)
CORS(app)
